package com.ruoyi.socket.util;

import com.google.common.collect.Lists;

import java.io.*;
import java.util.List;

/***
 *  @description
 *  @author zhao
 *  @date: 2020/6/12
 *  @version: v1.0
 */
public class FileUtils {

    private static String filePath = "proto.log";

    public static void writeByte(String bytes, String filePath) throws Exception {
        if (null == filePath) {
            filePath = "proto.log";
        }
        FileWriter fw = null;
        try {
            //如果文件存在，则追加内容；如果文件不存在，则创建文件
            File f = new File(filePath);
            fw = new FileWriter(f, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        PrintWriter pw = new PrintWriter(fw);
        pw.println(bytes);
        pw.flush();
        try {
            fw.flush();
            pw.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    public static List<String> readFileByCharUseBuffer(String filePath) {
        long beginTime = System.currentTimeMillis();
        List<String> data = Lists.newArrayList();
        if (null == filePath) {
            filePath = "proto.log";
        }
        File file = new File(filePath);
        String string;

        try {
            FileReader fileReader = new FileReader(file);
            BufferedReader buffedReader = new BufferedReader(fileReader);
            while (null != (string = buffedReader.readLine())) {
                data.add(string);
            }
            buffedReader.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("readFileByCharUseBuffer method costs :" + (endTime - beginTime) / 1000);

        return data;
    }


}
